<?php

class Model_ZendDb_Paginator_SearchBox extends Zend_Paginator_Adapter_DbSelect 
{
	/**
     * @var Model_ZendDb_DbTable_Feed
     */
    private $_feed;
    
    /**
     * @var Model_ZendDb_DbTable_News
     */
    private $_news;
    
    public function __construct(Zend_Db_Select $select)
    {
        parent::__construct($select);
        
        $this->_feed = new Model_ZendDb_DbTable_Feed();
        $this->_news = new Model_ZendDb_DbTable_News();
    }
    
    /**
     * @param $offset
     * @param $itemCountPerPage
     * @return array of Model_Entity_Category
     */
    public function getItems($offset, $itemCountPerPage) {
        $resultSet = parent::getItems($offset, $itemCountPerPage);
        
        $entries = array();
        foreach ($resultSet as $row) {
        	$box = new Model_Entity_Homepagebox($row);
        	
        	$feed = new Model_Entity_Feed(
        	   $this->_feed->find($row['feed_oid'])->current()->toArray());
        	
        	$newsRowset = $this->_news->fetchAll("feed_oid = {$row['feed_oid']}",
        	                                     array('oid DESC'), '5');
        	$newsArray = array();
        	foreach($newsRowset as $newsRow) {
        	   $news = new Model_Entity_News($newsRow->toArray());
        	   
        	   $newsArray[] = $news;
        	}
        	
        	$feed->setNews($newsArray);
        	$box->setFeed($feed);
            $entries[] = $box;
        }
        
        return $entries;
    }
    
    /**
     * Returns the total number of rows in the result set.
     *
     * @return integer
     */
    public function count()
    {
        return parent::count();
    }
}
